package com.homwee.factory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.homwee.factory.util.Contants;
import com.homwee.listMenu.util.ExtraAdapter;
import com.homwee.tvap.common.ap.ComSettingManager;
import com.homwee.tvap.common.srvfunc.FactorySetting;
import com.mediatek.twoworlds.factory.MtkTvFApiInformation;
import com.mediatek.twoworlds.factory.MtkTvFApiSystem;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.SystemProperties;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

public class FactoryExtraInfoActivity extends Activity {
	private String[] names=null;
	private Object[] values=null;

	private String[] names2=null;
	private Object[] values2=null;

	private String[] names3=null;
	private String[] values3=null;

	public static FactoryExtraInfoActivity instance=null;
	public ComSettingManager mcomSettingManager;
	public FactorySetting factorySetting;

	ExtraAdapter adapter=null;
	ExtraAdapter page2adapter=null;
	ExtraAdapter page3adapter=null;
	ListView listview=null;
	ListView page2listview=null;
	ListView page3listview=null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		instance=this;
		setContentView(R.layout.menu_list_extra);

		initData();

	}
	private void initData() {
		// TODO Auto-generated method stub
		mcomSettingManager=ComSettingManager.getInstance(this);
		factorySetting = mcomSettingManager.getFactory();

		String platform=SystemProperties.get("ro.board.platform", "");
		String productName=SystemProperties.get("ro.product.name", "");
		String fingerPrint=SystemProperties.get("ro.build.fingerprint", "");
		String projectId=factorySetting.getCurProjectIndex();
		String panel=factorySetting.getPanelName();
		String keyPad=getKeyPad(platform);
		String wifiMacAddr=factorySetting.getMAC();
		//String ethMacAddr=MtkTvFApiSystem.getInstance().readFileOneLine("/sys/devices/virtual/net/eth0/address",
		//		18,0);
		String deviceId=factorySetting.getDeviceID();
		String hdcpKey1=factorySetting.isHDCPKey1XExist()?"OK":"NG";
		String widevine=factorySetting.isWidevineKeyExist()?"OK":"NG";
		String keyMaster=factorySetting.isKeyMasterExist()?"OK":"NG";
		String playReady=factorySetting.isPlayReadyKeyExist()?"OK":"NG";
		String hashKey=factorySetting.isHashKeyExist()?"OK":"NG";
		String hdcpKey2=factorySetting.isHDCPKey2XExist()?"OK":"NG";
		String netfilxKey=factorySetting.isNETFLIXKeyExist()?"OK":"NG";
		String ciKey=factorySetting.isDTVCIPlusKeyExist()?"OK":"NG";
		String ipAddr=factorySetting.getIPAdress();
		String swVersion=SystemProperties.get("vendor.cus.product.fw.version", "");
		String buildTime=SystemProperties.get("ro.vendor.build.date", "");
		String tvWorkTime=factorySetting.getTvWorkTime();

		String aqVersion=factorySetting.getAQVersion();
		String irModel=MtkTvFApiInformation.getInstance().getIniParameter(Contants.PATH_CUSTOM_FUNC_INI,"RemoteControlType:mRemote1");//(factorySetting.getIrModel()).replace("/", "\n");
		String chip=getChip();
		String totalRam = FactorySetting.getTotalRam();
		String totalRom = String.valueOf(FactorySetting.getTotalRom());
		String sscSpan=String.valueOf(factorySetting.getLvdsSSCSpan());
		String sscStep=String.valueOf(factorySetting.getLvdsSSCStep());
		String swing=String.valueOf(factorySetting.getLvdsSwing());
		String sscEnable=String.valueOf(factorySetting.getLvdsSSCEnable());
		String miuEnable0=String.valueOf(factorySetting.getMIUEnable(0));
		//String miuEnable1=String.valueOf(factorySetting.getMIUEnable(1));
		String miuMod0=String.valueOf(factorySetting.getMIUModulation(0));
		//String miuMod1=String.valueOf(factorySetting.getMIUModulation(1));
		String miuPer0=String.valueOf(factorySetting.getMIUPercentage(0));
		//String miuPer1=String.valueOf(factorySetting.getMIUPercentage(1));

		String pvrEnable=MtkTvFApiInformation.getInstance().getIniParameter(
				Contants.PATH_CUSTOM_FUNC_INI, Contants.FACTORY_PVR_ENABLE);
		String timeShiftEnable=MtkTvFApiInformation.getInstance().getIniParameter(
				Contants.PATH_CUSTOM_FUNC_INI, Contants.FACTORY_TIMESHIFT_ENABLE);
		String advanceEnergy=MtkTvFApiInformation.getInstance().getIniParameter(
				Contants.PATH_CUSTOM_FUNC_INI, Contants.FACTORY_ADVANCEDENERGY_ENABLE);
		String hbbtvEnable=MtkTvFApiInformation.getInstance().getIniParameter(
				Contants.PATH_CUSTOM_FUNC_INI, Contants.FACTORY_HBBTV_ENABLE);
		String gingaEnable=MtkTvFApiInformation.getInstance().getIniParameter(
				Contants.PATH_CUSTOM_FUNC_INI, Contants.FACTORY_GINGA_ENABLE);
		String txtEnable=MtkTvFApiInformation.getInstance().getIniParameter(
				Contants.PATH_CUSTOM_FUNC_INI, Contants.FACTORY_TELETEXT_ENABLE);
		String mheg5Enable=MtkTvFApiInformation.getInstance().getIniParameter(
				Contants.PATH_CUSTOM_FUNC_INI, Contants.FACTORY_MHEG5_ENABLE);
		String mheg5Name=MtkTvFApiInformation.getInstance().getIniParameter(
				Contants.PATH_CUSTOM_FUNC_INI, Contants.FACTORY_MHEG5_PLATFORMNAME);
		String ciEnable=MtkTvFApiInformation.getInstance().getIniParameter(
				Contants.PATH_CUSTOM_FUNC_INI, Contants.FACTORY_CI_ENABLE);
		String oadEnable=MtkTvFApiInformation.getInstance().getIniParameter(
				Contants.PATH_CUSTOM_FUNC_INI, Contants.FACTORY_OAD_ENABLE);
		String autoSleep=MtkTvFApiInformation.getInstance().getIniParameter(
				Contants.PATH_CUSTOM_FUNC_INI, Contants.FACTORY_AUTOSLEEP);
		String dvbScanT=MtkTvFApiInformation.getInstance().getIniParameter(
				Contants.PATH_CUSTOM_FUNC_INI, Contants.FACTORY_DVB_SCAN_T);
		String dvbScanC=MtkTvFApiInformation.getInstance().getIniParameter(
				Contants.PATH_CUSTOM_FUNC_INI, Contants.FACTORY_DVB_SCAN_C);
		String dvbScanS=MtkTvFApiInformation.getInstance().getIniParameter(
				Contants.PATH_CUSTOM_FUNC_INI, Contants.FACTORY_DVB_SCAN_S);
		String eslEnable=(MtkTvFApiInformation.getInstance().getIniParameter(
				Contants.PATH_CUSTOM_FUNC_INI, Contants.FACTORY_ESL_ENABLE));
		String masterPin=MtkTvFApiInformation.getInstance().getIniParameter(
				Contants.PATH_CUSTOM_FUNC_INI, Contants.FACTORY_CUSFUN_MASTERPIN);
		String bootType=MtkTvFApiInformation.getInstance().getIniParameter(
				Contants.PATH_CUSTOM_FUNC_INI, Contants.FACTORY_CUSFUN_BOOTTYPE);
		String atvonly=MtkTvFApiInformation.getInstance().getIniParameter(
				Contants.PATH_CUSTOM_FUNC_INI, Contants.FACTORY_CUSFUN_ATVONLY);
		String tts=MtkTvFApiInformation.getInstance().getIniParameter(
				Contants.PATH_CUSTOM_FUNC_INI, Contants.FACTORY_CUSFUN_TTS);
		String vChip=MtkTvFApiInformation.getInstance().getIniParameter(
				Contants.PATH_CUSTOM_FUNC_INI, Contants.FACTORY_CUSFUN_VCHIP);
		String tconEnable=factorySetting.getTconEnable()+"";

		if(eslEnable==null||eslEnable.isEmpty()){
			eslEnable=String.valueOf(0);
		}
		if(advanceEnergy==null||advanceEnergy.isEmpty()){
			advanceEnergy=String.valueOf(0);
		}

		names=new String[]{"Platfrom","ProductName","FingerPrint","Project id","Panel","Device id"
				,"MAC Address","HDCP Key 1","HDCP Key 2","Widevine","KeyMaster",
				"PlayReady","HashKey","NETFILX","CI","IP","SW Version","Build Time","TvWorkTime"};
		values=new Object[]{platform,productName,fingerPrint,projectId,panel,deviceId,
				wifiMacAddr,hdcpKey1,hdcpKey2,widevine,keyMaster,
				playReady,hashKey,netfilxKey,ciKey,ipAddr,swVersion,buildTime,tvWorkTime};

		names2=new String[]{"Chip","Key Pad","AQ Version","Lvds SSC Enable","Lvds SSC Span",
				"Lvds SSC Step","Lvds SWing","SSC MIU0","MIU0 Modulation","MIU0 Percentage",
				" ","totalMemory",
				"totalStorage"
				};
		values2=new Object[]{chip,keyPad,aqVersion,sscEnable,sscSpan,sscStep,swing,miuEnable0,
				miuMod0,miuPer0," ",totalRam,totalRom};

		names3=new String[]{"PVR","Timeshift","OAD","AutoSleep","EPOP","AdvancedEnergy","MasterPin","BootType",
				" ","HBBTV","TeleText","MHEG5","MHEG5 Name","CI","DVB-T","DVB-C","DVB-S","ATV Only",
				" ","GINGA",
				" ","TTS","VChip",
				" ","Tconless"};
		values3=new String[]{pvrEnable,timeShiftEnable,oadEnable,autoSleep,eslEnable,advanceEnergy,masterPin,bootType,
				" ",hbbtvEnable,txtEnable,mheg5Enable,mheg5Name,ciEnable,dvbScanT,dvbScanC,dvbScanS,atvonly,
				" ",gingaEnable,
				" ",tts,vChip,
				" ",tconEnable};

		for(int i=0;i<values.length;i++){
			if(values[i]==null||values.length==0){
				values[i]="NG";
			}
		}

		for(int i=0;i<values2.length;i++){
			if(values2[i]==null){
				values2[i]="null";
			}
		}

		for(int i=0;i<values3.length;i++){
			if(TextUtils.isEmpty(values3[i])){
				values3[i]="null";
			}
		}

		List<Map<String,Object>> listitem=new ArrayList<Map<String,Object>>();
		for(int i=0;i<names.length;i++){
			Map<String,Object> showitem=new HashMap<String,Object>();
			showitem.put("names", names[i]);
			showitem.put("values", values[i]);
			listitem.add(showitem);
		}
		List<Map<String,Object>> page2listitem=new ArrayList<Map<String,Object>>();
		for(int i=0;i<names2.length;i++){
			Map<String,Object> showitem=new HashMap<String,Object>();
			showitem.put("names", names2[i]);
			showitem.put("values", values2[i]);
			page2listitem.add(showitem);
		}

		List<Map<String,Object>> page3listitem=new ArrayList<Map<String,Object>>();
		for(int i=0;i<names3.length;i++){
			Map<String,Object> showitem=new HashMap<String,Object>();
			showitem.put("names", names3[i]);
			showitem.put("values", values3[i]);
			page3listitem.add(showitem);
		}

		adapter =new ExtraAdapter(listitem, getApplicationContext(),0);
		listview=(ListView)findViewById(R.id.itemListViewExtra);
		listview.setAdapter(adapter);

		page2adapter=new ExtraAdapter(page2listitem, getApplicationContext(),0);
		page2listview=(ListView)findViewById(R.id.page2itemListViewExtra);
		page2listview.setAdapter(page2adapter);

		page3adapter=new ExtraAdapter(page3listitem, getApplicationContext(),1);
		page3listview=(ListView)findViewById(R.id.page3itemListViewExtra);
		page3listview.setAdapter(page3adapter);

	}


	private String getKeyPad(String platform) {
		// TODO Auto-generated method stub
		String str="";
		String keyPadNum="";
		//String KEYPAD1="{0x10,0x2F,0x4D,0x71,0x92,0xAB,0xC3,0xE7}";
		//String KEYPAD5="{0x0F,0x4B,0x88,0xAA,0xDE,0x00,0x00,0x00}";
		//String KEYPAD7="{0x0F,0x2E,0x40,0x6A,0x88,0xAA,0xDE,0x00}";

		if(platform.equalsIgnoreCase("m7632")||platform.equalsIgnoreCase("m7332")){
			str=MtkTvFApiInformation.getInstance().getIniParameter(
                Contants.FACTORY_EXTRA_BOARD_PATH, "BOARD_KEYPAD:BOARD_KEYPAD_CH_1_KEY_THRESHOLD");

			keyPadNum=MtkTvFApiInformation.getInstance().getIniParameter(
					Contants.FACTORY_EXTRA_BOARD_PATH,"BOARD_KEYPAD:BOARD_KEYPAD_CH_1_KEY_LEVEL_NUM");
		}
		else if(platform.equalsIgnoreCase("mt5867")){
			str=MtkTvFApiInformation.getInstance().getIniParameter(
	            Contants.FACTORY_EXTRA_BOARD_PATH, "BOARD_KEYPAD:BOARD_KEYPAD_CH_0_KEY_THRESHOLD");

			keyPadNum=MtkTvFApiInformation.getInstance().getIniParameter(
					Contants.FACTORY_EXTRA_BOARD_PATH,"BOARD_KEYPAD:BOARD_KEYPAD_CH_0_KEY_LEVEL_NUM");
		}
		str=str.replaceAll("\\s*", "");
		keyPadNum=keyPadNum+":"+str;
		return keyPadNum;
	}

	public String getHashKeyMD5() {
        ArrayList<Byte> hashKeyCode=new ArrayList<Byte>();
        hashKeyCode=MtkTvFApiSystem.getInstance().readFileByte("/vendor/tvcertificate/hashkey.sign",
				306,0);

        byte[] byteHashKey=new byte[hashKeyCode.size()];
        String str=hashKeyCode.toString();
        for(int i=0;i<hashKeyCode.size();i++){
        	byteHashKey[i]=hashKeyCode.get(i).byteValue();
        }

        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(byteHashKey);
            byte[] encryption = md5.digest();

            StringBuffer strBuf = new StringBuffer();
            for (int i = 0; i < encryption.length; i++) {
                if (Integer.toHexString(0xff & encryption[i]).length() == 1) {
                    strBuf.append("0").append(Integer.toHexString(0xff & encryption[i]));
                } else {
                    strBuf.append(Integer.toHexString(0xff & encryption[i]));
                }
            }

            return strBuf.toString();
        } catch (NoSuchAlgorithmException e) {
            return "";
        }

    }

	public String getChip(){
		String hashKeyMD5=getHashKeyMD5();
		switch (hashKeyMD5){
			case "6f9fff9b63f6965b30b74ca0d5eaeeda":
				return "MT9216AAAT/CBZA_noDBX";
			case "700132a3d97ea3374df3e551afff2535":
				return "MT9216AAAT/CBZA_DBX";
			case "5ba1a7f40be02ac9c4a4f6a097cefdfc":
				return "MT9216AAAT/DBZA_noDBX";
			case "caff8b21054a320188ec95328225b371":
				return "MT9216AAAT/DBZA_DBX";
			case "894721abbf2758f8707f8cfd89bd281d":
				return "MT9612IAAT.ACZA_DBX_DV_DA_noHDR10P_HBBTV2.0";
			case "bf465e88f12682e2c6411ee9f8d76256":
				return "MT9612IAAT.DCZA_DBX_DV_DA_noHDR10P_noHBBTV";
			case "608ebe96a7ef4f1435a99129d5149032":
				return "MT9612IAAT.DCZA_noDBX_DV_DA_noHDR10P_noHBBTV";
			case "9854c54e910e59597cb71e265975da06":
				return "MT9612IAAT-EBZA_DBX_noDV_noDA_noHBBTV";
			case "7b123cf8c75b2875a2e1d618bbde7a38":
				return "MT9612IAAT-GCZA_DBX_DV_DA_HBBTV";
			case "b09a8b0d1781d275252de1099a100f05":
				return "MT9612IAAT-GCZA_noDBX_DV_DA_HBBTV";
			case "9d6912ecc2dd16999dcb0567e0dce2bc":
				return "MT9612IAAT-MBZA_DBX_noDV_noDA_HBBTV";
			case "af6493dd0970c14a0171625f1d45af78":
				return "MT9612IAAT-MBZA_noDBX_noDV_noDA_HBBTV";
			case "738e8b39f5dcde4f8b9600810ad3b6f7":
				return "MT9216AAAT-FBZA-DBX";
			case "122e118a1d55d13d7c797550c31cd1c7":
				return "MT9216AAAT-FBZA";
			case "edfb8953e57639365304bb31e37c8e1b":
				return "MT9612IAAT-ICZA";
			case "6a27041490fae07bbe5d193710628d60":
				return "MT9612IAAT-ICZA_DBX";
			case "9eb0901bf95f09aeaec1810f44b728a8":
				return "MT9216AAAT-ZBZA";
			case "3f994c61c9724aa0db37f3ca3228aedf":
				return "MT9216AAAT-ZBZA_DBX";
			case "e3b1c22fb543ce47c54108d13be05d01":
				return "MT9612IAAT-HCZA_DBX";
			case "995d3366fda6cc9b05fb5bd78a4de5b1":
				return "MT9602KAAT_FBZA_wiHBBTV_HDR10_DBX";
			case "6128edcb2632001ad6ff5850a9a55cc8":
				return "MT9612IAAT.DCZA_DBX_DVIQ_DA_HDR10P_noHBBTV";
			case "a0bcae7bf2cf227061531421e7a73619":
				return "MT9612IAAT.DCZA_noDBX_DVIQ_DA_HDR10P_noHBBTV";
			case "130be98534db5e09889c1d9a46544c01":
				return "MT9612IAAT.DCZA_noDBX_DV_DA_HDR10P_noHBBTV";
			case "157cc06b5936221df7ae44ef01df7798":
				return "MT9612IAAT.ACZA_noDBX_DV_DA_noHDR10P_HBBTV2.0";
			case "0b5353bae4c4ce5ee77dca3ab4bd4645":
				return "MT9612IAAT.ACZA_DBX_DVIQ_DA_HDR10P_HBBTV2.0";
			case "e74870e09bca22f6eb8f9d682c27d5cc":
				return "MT9612IAAT.ACZA_noDBX_DVIQ_DA_HDR10P_HBBTV2.0";
			case "360c7bd0c92dbe95822c0564c722dc58":
				return "MT9612IAAT.ACZA_noDBX_DV_DA_HDR10P_HBBTV2.0";
			case "611ef6cbb5d1cad3109255402b4a2c6e":
				return "MT9612IAAT.GCZA_DBX_DV_DA_HDR10P_HBBTV2.0";
			case "dc8f1f956846062174acb8c17e08cb7c":
				return "MT9612IAAT.GCZA_DBX_DVIQ_DA_HDR10P_HBBTV2.0";
			case "a13eb79f7cca00294ca125edbe707aa5":
				return "MT9612IAAT.DCZA_DBX_DVIQ_DA_noHDR10P_noHBBTV";
			case "134d22621ab8333e16b1b6f84a08b9ee":
				return "MT9612IAAT.DCZA_noDBX_DVIQ_DA_noHDR10P_noHBBTV";
			case "589c0dfb4ccda10d5c8a7d908c45e507":
				return "MT9612IAAT.ACZA_DBX_DVIQ_DA_noHDR10P_HBBTV2.0";
			case "068cf46aaa76d9b7829eed8694955f55":
				return "MT9612IAAT.ACZA_noDBX_DVIQ_DA_noHDR10P_HBBTV2.0";
			case "46ab049d33efe78e26aae4f4fea5fc65":
				return "MT9602KAAT_FBZA_wiHBBTV_HDR10";
			case "a1d28eae25d159774601cc6175a43f2b":
				return "MT9602KAAT_FBZA_wiHBBTV";
			case "ceaa41133248967fd969807d6a21dea6":
				return "MT9602KAAT_BBZA_wiHDR10_DBX";
			case "be64f3af3b7f8d9cfe7ba4752f0d4430":
				return "MT9602KAAT_BBZA_wiHDR10";
			case "20aa53089e664a9c680c3a09fba62272":
				return "MT9602KAAT_BBZA";
			case "e4255eb5a81182e493cd0418c0797191":
				return "MT9602KAAT_ABZA_DBX";
			case "84dff97764aaa75c8743d5f7625b6e65":
				return "MT9602KAAT_ABZA";
			case "44e0296d6bb1b2dc3e2a3a052ecf6fed":
				return "MT9216HAAT_EBZA_DBX_HBBTV";
			case "e19b9d1a6dd3f8fbcddd06ef91354520":
				return "MT9216HAAT_EBZA_HBBTV_NoDBX";
			case "f7de92699c4042dffd3641cc6640d1ae":
				return "MT9216HAATFBZA-MS12-B_HEVC_AV1_TTS_DBX";
			case "fbebb5c6ec18e64b0f3ad53a531ceb51":
				return "MT9216HAATFBZA-MS12-B_HEVC_AV1_TTS_noDBX";
			case "c7a185e780c994b8894510fef7af68b5":
				return "MT9602LAATAB_DBX_HBBTV_HDR10P";
			case "e9dbf3c8ed6791cd63d248338893dbe4":
				return "MT9602LAATAB_noDBX_HBBTV_HDR10P";
			case "5acc0d0f2d7ed897d1ebb40b412e1c4c":
				return "MT9602LAATEB_DBX_noHBBTV_HDR10P";
			case "709dca72dd13ba07d6e1fd1da9e6e3d3":
				return "MT9602LAATEB_noDBX_noHBBTV_HDR10P";
			case "0349368c28753d600ebcc2a98b413d1a":
				return "MT9602LAATIB_DBX_noHBBTV_HDR10P_TCON";
			case "4d4b009a37bac44182f5e1b5365c184b":
				return "MT9602LAATIB_noDBX_noHBBTV_HDR10P_TCON";
			case "e854583267a6d78e0c9c3ef2ae5dba60":
				return "MT9602LAATBC_GINGA_DBX_DA_DV";
			case "1d7339e8f73d07f447887a6771976dda":
				return "MT9602LAATBC_GINGA_noDBX_DA_DV";
			case "b2f416907fd2e7c41931e2931cb55daf":
				return "MT9216HAATDB_DBX";
			case "bfa804578fbc2eb19ca488898421415ff":
				return "MT9216HAATDB_NODBX";
			case "3692cb8f4098d9dc1d4ae4f9cf54f2b2":
				return "MT9602KAATJB_DBX_JP_2K";
			case "7c21164760752cda701374e6a71f6c66":
				return "MT9602LAATFC_DBX_DV_DA_JP_4K";
			default:
				return hashKeyMD5;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		switch(keyCode){
		case KeyEvent.KEYCODE_MENU:
		case KeyEvent.KEYCODE_BACK:
			finish();
			break;
		}
		return super.onKeyDown(keyCode, event);
	}

}
